package com.mastercard.labs

import static org.junit.Assert.*

import grails.test.mixin.support.*


@TestFor(MerchantService)
@Mock([Merchant, Category, Country, MerchantService])
class MerchantServiceTests {

	def merchantService
	def merchant, category, country
	
	@Before
    void setUp() {
        println("-------- setUp ----------")
		merchantService = new MerchantService()
		
		category = new Category()
		category.name = "Test Category 1"
		category.save()
		
		country = new Country()
		country.code = "SG"
		country.name = "Singapore"
		country.save()
		
		merchant = new Merchant()
		merchant.name = "Test Company"
		merchant.country = country
		merchant.logo = ""
		merchant.contact = "87654321"
		merchant.categories = new ArrayList()
		merchant.categories.add(category)
		merchant.status = Merchant.Status.ACTIVE
		merchantService.saveMerchant(merchant)
    }

	@After
    void tearDown() {
        println("-------- tearDown ----------")
		merchant.delete()
		category.delete()
    }

	void testGetMerchantById() {
		println("-------- testGetMerchantById ----------")
		def mer = merchantService.getMerchantById(1)
		assert mer.name == "Test Company"
	}
	
    void testSearchMerchantByCondition() {
		println("-------- testSearchMerchantByCondition ----------")
		def list = merchantService.searchMerchantByCondition("Test", null, null, null)
		assert list.size() > 0
		
		if(list.size() > 0){
			def mer = list.get(0)
			assert mer.country.name == "Singapore"
		}
    }
	
	void testSearchMerchantForDashboard() {
		println("-------- testSearchMerchantForDashboard ----------")
		def list = merchantService.searchMerchantForDashboard()
		assert list.size() > 0
		
		if(list.size() > 0){
			def mer = list.get(0)
			assert mer.contact == "87654321"
		}
	}
	
	void testSaveMerchant() {
		println("-------- testSaveMerchant ----------")
		
		def mer = merchantService.getMerchantById(1)
		mer.contact = "12345678"
		merchantService.saveMerchant(mer)
		
		def tmp = merchantService.getMerchantById(1)
		assert tmp.contact == "12345678"
	}
}
